简书链接:原创轻松玩转vscodelaunchjsontasksjsonlinux启动c程序编译c程序
文章字数:318,阅读全文大约需要1分钟
编译c++网上的确有现成的插件,但是一点都不好用,比如需要传递个参数就蛋疼了。
技巧1: 自定义快捷键编译
技巧2:自定义编译命令
launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"cwd": "${workspaceFolder}",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "compile",
// "program3": "${workspaceRoot}/a.out",
"program": "${fileDirname}/${fileBasenameNoExtension}",
// "program": "${fileDirname}/${fileBasenameNoExtension}",
"stopAtEntry": false,
// "setupCommands": [
// {
// "description": "Enable pretty-printing for gdb",
// "text": "-enable-pretty-printing",
// "ignoreFailures": true
// }
// ],
// "customLaunchSetupCommands": [
// {
// "text": "target-run",
// "description": "run target",
// "ignoreFailures": false
// }
// ],
"launchCompleteCommand": "exec-run",
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe"
}
}
],
"configurations1": [
{
"name": " Launch C",
"type": "cppdbg",
"request": "launch",
"program": "{fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"preLaunchTask1": "compile",
"environment": [],
"externalConsole": false,
"MIMode": "gcc",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

tasks.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
//ctrl shift p choose task
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "shell",
"command": "echo compile exeover&g++ -g ${file} -o ${fileDirname}/${fileBasenameNoExtension}",
// "command": "g++ ${file} -o ${fileDirname}/${fileBasenameNoExtension}&echo compile exeover&${fileDirname}/${fileBasenameNoExtension}",
"cwd": "${workspaceFolder}",
"presentation": {
"reveal": "always",
"panel": "new"
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "compileRun",
"type": "shell",
// "command": "echo compile exeover&g++ -g ${file} -o ${fileDirname}/${fileBasenameNoExtension}",
"command": "g++ -g -g -Wall -std=c++11 ${file} -o ${fileDirname}/${fileBasenameNoExtension} -lpthread&&echo [-----compileSucc----]&&${fileDirname}/${fileBasenameNoExtension}&&echo [----executeSucc----]",
"args": [ //传递给命令的参数
{
// "value": "",
"quoting": "escape" //指定对内容的escape
}
],
"problemMatcher": [
],


"options": {
"cwd": "${fileDirname}",
"env": { //wsl的环境变量支持有问题,设置不生效
"MY_NAME": "aaaaaaa"
},
"shell": { //shell执行的一些配置
"executable": "bash",
"args": [
"-c"
]
},
},

"presentation": {
"echo": false,//如果 为false,怎么执行命令是不会提示的,但是 程序里面的控制信息是会执行的.
"reveal": "always",
// "reveal": "never",//揭露 总是 显示信息不然搞毛.
"focus": false,
"panel": "shared",//面板就是每次启动的时候是否新开 dedicated专用 ,shared保留之前
"clear": true,//每次变异 是否清屏.
"showReuseMessage": false// 不是,true就提示enter,否则 还是需要按enter 就是提示那个英文而已但是并不会自动关闭
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

cpp_properties这个配置需要微软的插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

快捷键启动指定的task实现编译操作

1
2
3
4
5
{
"key": "ctrl+b",
"command": "workbench.action.tasks.runTask",
"args": "compileRun"
}

我配置的所有快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 使用当前快捷键设置覆盖默认设置
[
{
"key": "alt+d",
"command": "editor.action.deleteLines",
"when": "editorFocus"
},
{
"key": "alt+w",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus"
},
{
"key": "alt+s",
"command": "editor.action.moveLinesDownAction",
"when": "editorTextFocus"
},
{
"key": "ctrl+shift+f",
"command": "editor.action.format",
"when": "editorTextFocus"
},
{
"key": "f7",
"command": "workbench.action.debug.stepOut",
"when": "inDebugMode"
},
{
"key": "alt+u",
"command": "cursorHome",
"when": "editorTextFocus"
},
{
"key": "alt+o",
"command": "cursorEnd",
"when": "editorTextFocus"
},
{
"key": "ctrl+j",
"command": "workbench.action.gotoLine"
},
{
"key": "f6",
"command": "workbench.action.debug.stepOver",
"when": "inDebugMode"
},
{
"key": "shift+s",
"command": "editor.action.insertLineAfter",
"when": "editorTextFocus"
},
{
"key": "shift+w",
"command": "editor.action.insertLineBefore",
"when": "editorTextFocus"
},
{
"key": "alt+n",
"command": "editor.action.copyLinesDownAction",
"when": "editorTextFocus"
},
{
"key": "f5",
"command": "workbench.action.debug.stepInto",
"when": "inDebugMode"
},
{
"key": "alt+i",
"command": "cursorUp",
"when": "textInputFocus&& !parameterHintsVisible &&!suggestWidgetVisible"
},
{
"key": "alt+k",
"command": "cursorDown",
"when": "textInputFocus"
},
{
"key": "alt+j",
"command": "cursorLeft",
"when": "textInputFocus"
},
{
"key": "alt+l",
"command": "cursorRight",
"when": "textInputFocus"
},
{
"key": "alt+k",
"command": "showNextParameterHint",
"when": "editorTextFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
{
"key": "alt+k",
"command": "showPrevParameterHint",
"when": "editorTextFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
{
"key": "alt+k",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "alt+i",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "alt+/",
"command": "editor.action.triggerSuggest",
"when": "editorHasCompletionItemProvider && textInputFocus && !editorReadonly"
},
{
"key": "shift+alt+r",
"command": "renameFile",
"when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus"
},
{
"key": "alt+shift+r",
"command": "editor.action.codeAction",
"args": {
"kind": "refactor.extract.function"
},
"when": "editorTextFocus"
},
{
"key": "f2",
"command": "-renameFile",
"when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus"
},
{
"key": "alt+shift+r",
"command": "editor.action.changeAll",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+b",
"command": "workbench.action.tasks.runTask",
"args": "compileRun"
}
]

我的项目源码
https://github.com/qssq/linuxcpp/tree/master/.vscode